using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
namespace THSLib
{
   public class RegistryWorker
    {
        #region --Constant--

           public const String strExpriedDate = "ExpireDate";
           public const String ProductLimited = "ProductLimited";
           public const String CustomerLimited = "CustomerLimited";
          
        #endregion
        #region --Veriables--
           private bool showError = false;
           private String subKey = "SOFTWARE\\"+Application.ProductName.ToUpper();
           private RegistryKey baseRegistryKey = Registry.LocalMachine;
        #endregion
        #region --Properties--
               public String SubKey
               {
                   get { return subKey;}
                   set { subKey = value; }
               }
               public RegistryKey BaseRegistryKey 
               {
                   get { return baseRegistryKey; }
                   set { baseRegistryKey = value; }
               }
        #endregion
                       #region --Functions
                       /// <summary>
               ///   Read key name from registry
               /// </summary>
               /// 
               public String Read(String strKeyName)
               {
                   RegistryKey rk = baseRegistryKey;
                   RegistryKey sk1 = rk.OpenSubKey(subKey);
                   if(sk1==null)
                       return null;
                   try
                   {
                       return (String)sk1.GetValue(strKeyName.ToUpper());
                   }
                   catch (System.Exception ex)
                   {
                       ShowErrMessage(ex, "Reading registry "+ strKeyName.ToUpper());
                       return null;
                   }
               }
       public bool Write(string KeyName, object Value)
       {
           try
           {
               RegistryKey rk = baseRegistryKey;
               RegistryKey sk1 = rk.CreateSubKey(subKey);

               sk1.SetValue(KeyName, Value);

               return true;
           }
           catch (Exception e)
           {
               ShowErrMessage(e, "Writing registry " + KeyName);
               return false;
           }
       }
               /// <summary>
               ///   write to registry
               /// </summary>
               /// 
               public bool Write(String strKeyName,String strValue,object objValue)
               {
                   try
                   {
                  
                   RegistryKey rk = baseRegistryKey;
                   RegistryKey sk = rk.CreateSubKey(strKeyName);
                   sk.SetValue(strKeyName.ToUpper(), objValue);
                   return true;
               }
               catch (System.Exception ex)
               {
                   {
                       ShowErrMessage(ex, "Write registry " + strKeyName.ToUpper());
                       return false;
                   }
               }
               }

               /// <summary>
               ///   Delete registry from 
               /// </summary>
               public bool Delete(String strKeyName)
               {
                   try
                   {

                       RegistryKey rk = baseRegistryKey;
                       RegistryKey sk = rk.CreateSubKey(strKeyName);
                       if (sk == null)
                           return true;
                       else
                           sk.DeleteValue(strKeyName);
                       return true;

                       
                   }
                   catch (System.Exception ex)
                   {
                       {
                           ShowErrMessage(ex, "Delete registry " + strKeyName.ToUpper());
                           return false;
                       }
                   }
               }
               /// <summary>
               ///   Delete tree registry 
               /// </summary>
               /// 
               public bool DeleteSubKeyTree ()
               {
                   try
                   {
                       RegistryKey rk = baseRegistryKey;
                       RegistryKey sk = rk.OpenSubKey(subKey);
                       if (sk != null)
                           rk.DeleteSubKeyTree(subKey);
                       return true;
                   }catch(Exception ex)
                   {
                       ShowErrMessage(ex, "Delete registry " + subKey);
                       return false;
                   }
               }
               /// <summary>
               ///   count subKey from registry
               /// </summary>
               /// 

               public int SubKeyCount()
               {
                   try
                   {
                       RegistryKey rk = baseRegistryKey;
                       RegistryKey sk = rk.OpenSubKey(subKey);
                       if (sk != null)
                           return rk.SubKeyCount;
                      else
                           return 0;
                   }
                   catch (Exception ex)
                   {
                       ShowErrMessage(ex," Received SubKeys of " + subKey);
                       return 0;
                   }
               }
               /// <summary>
               ///   Count value from registry
               /// </summary>
               /// 
               public int ValueCount()
               {
                   try
                   {
                       RegistryKey rk = baseRegistryKey;
                       RegistryKey sk = rk.OpenSubKey(subKey);
                       if (sk != null)
                           return rk.ValueCount;
                       else
                           return 0;
                   }
                   catch (Exception ex)
                   {
                       ShowErrMessage(ex, " Received SubKeys of " + subKey);
                       return 0;
                   }
               }


               /// 
               /// <summary>
               ///   show error message box
               /// </summary>
               /// 
               private void ShowErrMessage(Exception ex,String strTitle)
               {
                   if (showError)
                   {
                       MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                   }
               }
        #endregion
   }
}
